﻿namespace Hidistro.UI.SaleSystem.Tags
{
    using Hidistro.Core;
    using Hidistro.Entities;
    using Hidistro.Entities.Comments;
    using Hidistro.SaleSystem.Comments;
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml;
    using System.Text.RegularExpressions;

    public class Common_SubjectArticle : WebControl
    {
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.RendHtml());
        }

        private void RenderHeader(XmlNode node, StringBuilder sb)
        {
            sb.AppendLine("<div class=\"article_hd\">");
            sb.AppendLine("<h2>");
            if (!string.IsNullOrEmpty(node.Attributes["ImageTitle"].Value))
            {
                sb.AppendFormat("<img src=\"{0}\" />", Globals.ApplicationPath + node.Attributes["ImageTitle"].Value);
               
                     
                    
                

            }
            if (!string.IsNullOrEmpty(node.Attributes["Title"].Value))
            {
                sb.Append(node.Attributes["Title"].Value);
            }
            sb.AppendLine("</h2>");
            sb.AppendLine("</div>");
        }

        public string RendHtml()
        {
            XmlNode node = TagsHelper.FindCommentNode(this.CommentId, "article");
            StringBuilder sb = new StringBuilder();
            if (node != null)
            {
                sb.AppendFormat("<div class=\"article cssEdite\" type=\"article\" id=\"comments_{0}\" >", this.CommentId).AppendLine();
                this.RenderHeader(node, sb);
                sb.AppendLine("<div class=\"article_bd\">");
                if (!string.IsNullOrEmpty(node.Attributes["AdImage"].Value))
                {
                 
                    sb.AppendFormat("<div class=\"article_ad\"><img src=\"{0}\" /></div>", node.Attributes["AdImage"].Value).AppendLine();
                    int id = 0;
                    int.TryParse(node.Attributes["CategoryId"].Value, out id);
                    ArticleInfo article = CommentBrowser.GetArticle(id);
                    sb.AppendFormat("<div class='contmens'><h1>{1}</h1>:   <b class='articleCount'><a target=\"_blank\" href=\"{0}\">{2}</a></b></div>", Globals.GetSiteUrls().UrlData.FormatUrl("ArticleDetails", new object[] { article.ArticleId }),article.Title, FilterHTML(article.Content).Substring(0,150)+"…详细>>").AppendLine(); 
                }
                int result = 0;
                int num2 = 0;
                int.TryParse(node.Attributes["CategoryId"].Value, out result);
                int.TryParse(node.Attributes["MaxNum"].Value, out num2);
                IList<ArticleInfo> articleList = CommentBrowser.GetArticleList(result, num2);
                if ((articleList != null) && (articleList.Count > 0))
                {
                    sb.AppendLine("<div class=\"article_list\">");
                    sb.AppendLine("<ul>");
                    foreach (ArticleInfo info in articleList)
                    {
                        sb.AppendFormat("<li><span>·原创 |  </span><a target=\"_blank\" href=\"{0}\">{1}</a></li>", 
                        Globals.GetSiteUrls().UrlData.FormatUrl("ArticleDetails", new object[] { info.ArticleId }), info.Title).AppendLine();
                    }
                    sb.AppendLine("</ul>");
                    sb.AppendLine("</div>");
                }
                sb.AppendLine("</div>");
                sb.AppendLine("</div>");
            }
            return sb.ToString();
        }
        private string FilterHTML(string html)
        {
            System.Text.RegularExpressions.Regex regex1 =
                  new System.Text.RegularExpressions.Regex(@"<script[sS]+</script *>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex2 =
                  new System.Text.RegularExpressions.Regex(@" href *= *[sS]*script *:",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex3 =
                  new System.Text.RegularExpressions.Regex(@" no[sS]*=",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex4 =
                  new System.Text.RegularExpressions.Regex(@"<iframe[sS]+</iframe *>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex5 =
                  new System.Text.RegularExpressions.Regex(@"<frameset[sS]+</frameset *>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex6 =
                  new System.Text.RegularExpressions.Regex(@"<img[^>]+>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex7 =
                  new System.Text.RegularExpressions.Regex(@"</p>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex8 =
                  new System.Text.RegularExpressions.Regex(@"<p>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Regex regex9 =
                  new System.Text.RegularExpressions.Regex(@"<[^>]*>",
                  System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            html = regex1.Replace(html, ""); 
            html = regex2.Replace(html, "");    
            html = regex3.Replace(html, " _disibledevent=");  
            html = regex4.Replace(html, "");    
            html = regex5.Replace(html, ""); 
            html = regex6.Replace(html, ""); 
            html = regex7.Replace(html, "");    
            html = regex8.Replace(html, ""); 
            html = regex9.Replace(html, "");
    
            html = html.Replace("</strong>", "");
            html = html.Replace("<strong>", "");
            html = Regex.Replace(html, "[\f\n\r\t\v]", "");  
            return html;
        }



      

        public int CommentId { get; set; }
    }
}

